package com.lwj.liwanjia.adapter.indent;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.config.OnItemChildClickListener;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.view.activity.indent.UnpaidActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/22.
 */
//public class CompleteAdapter extends SuperBaseAdapter<String>
public class CompleteAdapter extends RecyclerView.Adapter<CompleteAdapter.MyViewHolder> {
    private Context mContext;
    private List<MyIndentBean.DataBean> list;
    private boolean flag;//用来判断是否是详情 true代表是
    private LayoutInflater inflater;
    private OnItemClickListener listener;
    private OnItemChildClickListener childListener;
    private OnClickPosition clickPosition;

    public void setClickPosition(OnClickPosition clickPosition) {
        this.clickPosition = clickPosition;
    }

    public void setChildListener(OnItemChildClickListener childListener) {
        this.childListener = childListener;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public CompleteAdapter(Context mContext, List<MyIndentBean.DataBean> list, boolean flag) {
        this.mContext = mContext;
        this.list = list;
        this.flag = flag;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (list.get(viewType).getTitle().equals("购物")) {
            View view = inflater.inflate(R.layout.unpaid_item, parent, false);
            CompleteAdapter.MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.llFukuan.setVisibility(View.GONE);
        holder.rlIndentBtn.setVisibility(View.VISIBLE);
        holder.tvDeleteIndent.setVisibility(View.VISIBLE);
        final MyIndentBean.DataBean.GoodsBean goods = list.get(position).getGoods();
        holder.tvState.setText(goods.getStatus());
        if (list.get(position).getTitle().equals("购物")) {
            if (goods.getStatus().contains("未同意") ) {
                holder.tvDeleteIndent.setText("删除订单");
                holder.tvQuxiaoIndent.setText("联系商家");
            }else {
                holder.tvDeleteIndent.setText("删除订单");
                if (goods.getAssess() == 0) {//待评价
                    holder.tvQuxiaoIndent.setText("评价");
                } else if (goods.getAssess() == 1) {//追加评价
                    holder.tvQuxiaoIndent.setText("追加评价");
                } else {//再次购买
                    //holder.tvQuxiaoIndent.setText("再次购买");
                    holder.tvQuxiaoIndent.setText("联系商家");
                }
            }
            holder.tvMarket.setText(goods.getShopname());
            holder.tvShangchengNumber.setText("共" + goods.getAmount() + "件商品 合计：");
            holder.tvShangchengPrice.setText("￥" + goods.getTotalpay() + "(含运费￥0.00)");
            Glide.with(mContext).load(goods.getShopimage()).error(R.mipmap.shop_image_2).into(holder.ivMarket);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            holder.rlvGouwuche.setLayoutManager(manager);
            holder.rlvGouwuche.setRefreshEnabled(false);
            holder.rlvGouwuche.setLoadMoreEnabled(false);
            ShopUnpaidAdater1 adapter = new ShopUnpaidAdater1(mContext, goods.getCommodity(), flag);
            holder.rlvGouwuche.setAdapter(adapter);
            adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, Object item, int position1) {
                    if(clickPosition!=null){
                        clickPosition.clickPosition(position);
                    }
                    Intent intent = new Intent(mContext, UnpaidActivity.class);
                    intent.putExtra("tradeId", list.get(position).getGoods().getTradeId());
                    mContext.startActivity(intent);
                }
            });
            if (listener != null) {
                holder.rlGouwu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.OnItemClick(v, position);
                    }
                });
            }
            if (childListener != null) {
                holder.tvDeleteIndent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        childListener.OnItemChildClick(v, goods.getStatus(), position);
                    }
                });
                holder.tvQuxiaoIndent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        childListener.OnItemChildClick(v, goods.getStatus(), position);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_gouwu)
        RelativeLayout rlGouwu;
        @BindView(R.id.iv_market)
        ImageView ivMarket;
        @BindView(R.id.tv_market)
        TextView tvMarket;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.rl_market)
        RelativeLayout rlMarket;
        @BindView(R.id.rlv_gouwuche)
        SuperRecyclerView rlvGouwuche;
        @BindView(R.id.line)
        View line;
        @BindView(R.id.tv_shangcheng_price)
        TextView tvShangchengPrice;
        @BindView(R.id.tv_shangcheng_number)
        TextView tvShangchengNumber;
        @BindView(R.id.rl_jiage)
        RelativeLayout rlJiage;
        @BindView(R.id.line1)
        View line1;
        @BindView(R.id.tv_minute)
        TextView tvMinute;
        @BindView(R.id.tv_second)
        TextView tvSecond;
        @BindView(R.id.ll_fukuan)
        LinearLayout llFukuan;
        @BindView(R.id.tv_quxiao_indent)
        TextView tvQuxiaoIndent;
        @BindView(R.id.tv_delete_indent)
        TextView tvDeleteIndent;
        @BindView(R.id.rl_indent_btn)
        RelativeLayout rlIndentBtn;
        @BindView(R.id.indent_btn)
        RelativeLayout indentBtn;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

//    private List<String> list;//商城
//    private Context mContext;
//    private boolean flag;//用来判断是否是详情 true代表是
//
//    public CompleteAdapter(Context context, List<String> data, boolean flag) {
//        super(context, data);
//        list = new ArrayList<>();
//        list.add("1");
//        this.flag = flag;
//        this.mContext = context;
//    }
//
//    @Override
//    protected void convert(BaseViewHolder holder, String item, int position) {
//        holder.setOnClickListener(R.id.tv_quxiao_indent, new OnItemChildClickListener());
//        holder.setText(R.id.tv_state, "已完成")
//                .setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.colorFF2D2E))
//                .setText(R.id.tv_quxiao_indent, "评价")
//                .setText(R.id.tv_delete_indent, "删除订单")
//                .setVisible(R.id.ll_fukuan, false)
//                .setVisible(R.id.rl_indent_btn, true)//订单按钮可见
//                //  .setVisible(R.id.tv_quxiao_indent, false)//当评价完后，评价按钮隐藏
//                .setVisible(R.id.tv_delete_indent, true);//左边按钮可见
//        if (item.equals("0") || item.equals("1") || item.equals("2")) {//商城类
//            SuperRecyclerView rlvGouwuche = holder.getView(R.id.rlv_gouwuche);
//            LinearLayoutManager manager = new LinearLayoutManager(mContext);
//            manager.setOrientation(LinearLayoutManager.VERTICAL);
//            rlvGouwuche.setLayoutManager(manager);
//            rlvGouwuche.setRefreshEnabled(false);
//            rlvGouwuche.setLoadMoreEnabled(false);
//            ShopUnpaidAdater adapter = new ShopUnpaidAdater(mContext, list, flag);
//            rlvGouwuche.setAdapter(adapter);
//            if (item.equals("1")) {
//                holder.setText(R.id.tv_quxiao_indent, "追加评价");
//            } else if (item.equals("2")) {
//                holder.setText(R.id.tv_quxiao_indent, "再次购买");
//            } else {
//                holder.setText(R.id.tv_quxiao_indent, "评价");
//            }
//            adapter.setOnItemClickListener(new OnItemClickListener() {
//                @Override
//                public void onItemClick(View view, Object item, int position) {
//                    Intent intent = new Intent(mContext, CompleteActivity.class);
//                    mContext.startActivity(intent);
//                }
//            });
//        } else if (item.equals("3") || item.equals("4") || item.equals("5")) {//帮我买，送，取
//            if (item.equals("3")) {//帮我送
//                holder.setText(R.id.tv_bangwo, "帮我送");
//                holder.setImageResource(R.id.iv_address1, R.mipmap.fahuodizhi)
//                        .setImageResource(R.id.iv_address2, R.mipmap.quhuodizhi);
//            } else if (item.equals("4")) {//帮我取
//                holder.setText(R.id.tv_bangwo, "帮我取");
//                holder.setImageResource(R.id.iv_address1, R.mipmap.quhuodizhi)
//                        .setImageResource(R.id.iv_address2, R.mipmap.shouhuodizhi);
//            } else {
//                holder.setText(R.id.tv_bangwo, "帮我买");
//                holder.setImageResource(R.id.iv_address1, R.mipmap.purchaseadd)
//                        .setImageResource(R.id.iv_address2, R.mipmap.songhuodizhi);
//            }
//        } else if (item.equals("7")) {//旅游
//            holder.setVisible(R.id.rl_daoyou, false);//取消导游费用栏
//        } else if (item.equals("9") || item.equals("10")) {//宾馆看房
//            if (item.equals("9")) {
//                holder.setImageResource(R.id.iv_shangjia_icon, R.mipmap.binguantouxiang)
//                        .setVisible(R.id.rl_binguan2, true)
//                        .setText(R.id.tv_shangjia_name, "速8宾馆");
//            } else {//看房
//                holder.setImageResource(R.id.iv_shangjia_icon, R.mipmap.touxiang_1)
//                        .setVisible(R.id.rl_binguan2, false)
//                        .setText(R.id.tv_shangjia_name, "JJFJFJFJF")
//                        .setText(R.id.tv_binguan_name, "龙湾区海滨房")
//                        .setText(R.id.tv_binguan_price, "￥3000.0元/月")
//                        .setText(R.id.tv_binguan_jiesao, "四周环绕着大海，每天清晨都会感受到一缕阳光。");
//            }
//        } else if (item.equals("11")) {//酒店外卖
//            SuperRecyclerView rlvJiudian = holder.getView(R.id.rlv_jiudian);
//            LinearLayoutManager manager = new LinearLayoutManager(mContext);
//            manager.setOrientation(LinearLayoutManager.VERTICAL);
//            rlvJiudian.setLayoutManager(manager);
//            rlvJiudian.setRefreshEnabled(false);
//            rlvJiudian.setLoadMoreEnabled(false);
//            JiudianAdapter jiudianAdapter = new JiudianAdapter(mContext, list);
//            rlvJiudian.setAdapter(jiudianAdapter);
//        } else if (item.equals("12")) {//搬家
//            holder.setImageResource(R.id.iv_shangjia_icon, R.mipmap.touxiang_2)
//                    .setText(R.id.tv_shangjia_name, "KFFFSDDD");
//        } else if (item.equals("13") || item.equals("14")) {//滴滴
//            if (item.equals("13")) {//预约今天
//                holder.setVisible(R.id.tv_yuyue, false);
//            } else {//预约明天
//                holder.setVisible(R.id.tv_yuyue, true);
//            }
//            holder.setImageResource(R.id.iv_shangjia_icon, R.mipmap.touxiang)
//                    .setText(R.id.tv_shangjia_name, "GGHFHJFHF");
//        }
//    }
//
//    @Override
//    protected int getItemViewLayoutId(int position, String item) {
//        if (item.equals("0") || item.equals("1") || item.equals("2")) {//商城
//            return R.layout.unpaid_item;
//        } else if (item.equals("3") || item.equals("4") || item.equals("5")) {//帮我买，送，取
//            return R.layout.unpaid_bangwosong_item;
//        } else if (item.equals("6")) {//导游
//            return R.layout.unorder_daoyou_item;
//        } else if (item.equals("7")) {//旅游
//            return R.layout.unpaid_lvyou_item;
//        } else if (item.equals("8")) {//寄快递
//            return R.layout.unpaid_kuaidi_item;
//        } else if (item.equals("9") || item.equals("10")) {//宾馆，看房
//            return R.layout.unpaid_binguan_item;
//        } else if (item.equals("11")) {//酒店外卖
//            return R.layout.unpaid_jiudian_item;
//        } else if (item.equals("12")) {//搬家
//            return R.layout.unpaid_banjia_item;
//        } else if (item.equals("13") || item.equals("14")) {//滴滴
//            return R.layout.unorder_car_item;
//        } else if (item.equals("15")) {//包间选择
//            return R.layout.unorder_jiudian_item;
//        }
//        return 0;
//    }
}
